package com.cwf.business.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.cwf.business.activity.login.LoginActivity;
import com.cwf.business.preferences.PreferencesUtils;
import com.cwf.business.R;
import com.cwf.business.permission.FePermissions;
import com.cwf.business.permission.PermissionCode;
import com.cwf.business.permission.PermissionGranted;
import com.cwf.business.permission.PermissionSettingDialog;
import com.cwf.business.util.MainUtil;

/**
 * Created by sam_chan on 2017/5/8.
 */

public class SplashActivity extends DFActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setContentView(R.layout.activity_splash);
        int requestWidth = getResources().getDisplayMetrics().widthPixels;
        int requestHeight = getResources().getDisplayMetrics().heightPixels;
        ImageView imageView = (ImageView) findViewById(R.id.ivSplash);
        imageView.setImageBitmap(MainUtil.scaleBitmap(getResources(), R.drawable.welcome_fe, requestWidth, requestHeight));

        FePermissions.with(SplashActivity.this)
                .permissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE})
                .rationaleMessage(getResources().getString(R.string.permission_msg_base_info))
                .requestCode(PermissionCode.BASE)
                .request();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        FePermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults, (requestCode1, permissions1, grantResults1, deniedMessage) -> new PermissionSettingDialog.Builder(SplashActivity.this)
                .setCancelable(false)
                .setMessage(getResources().getString(R.string.permission_msg_request_failed_base))
                .setNeutralText(getResources().getString(R.string.permission_text_go_setting))
                .setPositiveText(getResources().getString(R.string.permission_text_i_know))
                .setPositiveListener(v -> finish())
                .build()
                .show());
    }

    @PermissionGranted(PermissionCode.BASE)
    public void onBasePermissionGranted() {
        int delayTime = PreferencesUtils.getInstance().getPreferences(PreferencesUtils.EXTRA_DELAY_TIME, 1024);
        final boolean gestures = PreferencesUtils.getInstance().getPreferences(PreferencesUtils.SAVE_USER_INFO, true);
        new Handler().postDelayed(() -> {
            if (!gestures) {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                finish();
            } else {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
            finish();
            overridePendingTransition(R.anim.welcome_screen_enteranim, R.anim.welcome_screen_exitanim);
        }, delayTime);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
